package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@Slf4j
@RequestMapping("/dish")
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private DishFlavorService dishFlavorService;

    @Autowired
    private CategoryService categoryService;

    /*
    * 分页查询
    * */
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){

        log.info("page = {},pageSize = {},name = {}",page,pageSize,name);

        //构造分页查询构造器
        Page<Dish> pageInfo = new Page<>(page,pageSize);
        //拷贝数据后的完整的类用于最后数据的返回
        Page<DishDto> dishDtoPage = new Page<>();

        //构造条件构造器
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();

        //往条件构造器中添加条件
        queryWrapper.like(StringUtils.isNotEmpty(name),Dish::getName,name);

        queryWrapper.orderByDesc(Dish::getUpdateTime);

        dishService.page(pageInfo,queryWrapper);

        //对象拷贝
        //除了records剩下的都拷贝
        BeanUtils.copyProperties(pageInfo,dishDtoPage,"records");

        //先把pageInfo中的records拿出来
        List<Dish> records = pageInfo.getRecords();

        //将records中的每一个集合中都添加上categoryName属性
        List<DishDto> list = records.stream().map((item) -> {

            DishDto dishDto = new DishDto();

            BeanUtils.copyProperties(item, dishDto);

            Long categoryId = item.getCategoryId();//拿到分类的id
            //根据分类的id查询数据
            Category category = categoryService.getById(categoryId);
            if (category != null){

                String categoryName = category.getName();
                dishDto.setCategoryName(categoryName);

            }

            //返回一个属性都存在的类
            return dishDto;

        }).collect(Collectors.toList());

        //最后将处理后的records返回给dishDtoPage
        dishDtoPage.setRecords(list);


        return R.success(dishDtoPage);
    }

    /*
    * 修改菜品回显数据
    * */

    @GetMapping("/{id}")
    public R<DishDto> getById(@PathVariable Long id){

        log.info("id = {}",id);

        DishDto dishDto = dishService.mySetById(id);

        return R.success(dishDto);

    }

    /*
    * 修改菜品
    * */
    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto){
        log.info(dishDto.toString());

        dishService.myUpdateById(dishDto);

        return R.success("修改成功");

    }


    /*
    * 新增菜品
    * */

    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto){

        log.info(dishDto.toString());

        dishService.mySave(dishDto);

        return R.success("新增成功");

    }

    /*
    * 停售起售
    * */
    @PostMapping("/status/{status}")
    public R<String> updateStatus(@PathVariable Integer status, @RequestParam List<Long> ids){

        log.info(status.toString(),ids.toString());
        dishService.updateStatus(status,ids);

        return R.success("操作成功");

    }

    /*
    * 删除批量删除信息
    * */
    @DeleteMapping
    public R<String> delete(@RequestParam("ids") List<Long> ids){

        log.info(ids.toString());

        dishService.myDelete(ids);

        return R.success("删除成功");
    }

    /*
    * 新增套餐中的菜品添加
    * */
    @GetMapping("/list")
    public R<List<DishDto>> list(Dish dish){



        //构造查询条件
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();

        //等值匹配
        queryWrapper.eq(dish.getCategoryId()!=null,Dish::getCategoryId,dish.getCategoryId());

        //添加查询顺序
        queryWrapper.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);

        //查询后的结果返回到一个dish集合中
        List<Dish> dishList = dishService.list(queryWrapper);

        //如果菜品为停售状态的时候则不显示在菜单中
        for (int i = 0; i < dishList.size(); i++) {

            Dish dish1 = dishList.get(i);

            Integer status = dish1.getStatus();
            if (status == 0){
                dishList.remove(i);
            }
        }

        List<DishDto> dishDtoList = dishList.stream().map((item) -> {

            DishDto dishDto = new DishDto();

            //先把普通的属性都拷贝过去
            BeanUtils.copyProperties(item, dishDto);

            LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper = new LambdaQueryWrapper<>();

            lambdaQueryWrapper.eq(DishFlavor::getDishId, item.getId());

            List<DishFlavor> dishFlavors = dishFlavorService.list(lambdaQueryWrapper);

            dishDto.setFlavors(dishFlavors);

            return dishDto;

        }).collect(Collectors.toList());


        return R.success(dishDtoList);

    }


}
